package com.framework.base.util;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.framework.base.constant.WebConstants;

/**
 * @Copyright @ 2013
 * All right reserved
 * @version 创建时间：Created on 2013-4-26
 * @author 作者：Create by www.360buyli.com
 * @Email: 360buyli@gmail.com
 * @description 用于处理跳转的参数转化
 */

public class ParamsUrlUtil {
	public static String dealParamsUrl(HttpServletRequest httpRequest){
		
		StringBuffer paramsContent=new StringBuffer();
		//以下属性用于自定义排序参数
		 String orderFids="";//排序字段
		 String orderType="";//排序类型
		 String currentPage="";//当前页
		//1.取自定义的数据

		//2.取action的属数据
		orderFids=StringUtil.getStringFromObj(httpRequest.getAttribute(WebConstants.ORDERFIDS));
		orderType=StringUtil.getStringFromObj(httpRequest.getAttribute(WebConstants.ORDERTYPE));
		currentPage=StringUtil.getStringFromObj(httpRequest.getAttribute(WebConstants.CURRENTPAGE));
		//3.取参数数据

			 orderFids=StringUtil.getStringFromObj(httpRequest.getParameter(WebConstants.ORDERFIDS));
			 orderType=StringUtil.getStringFromObj(httpRequest.getParameter(WebConstants.ORDERTYPE));
			 currentPage=StringUtil.getStringFromObj(httpRequest.getParameter(WebConstants.CURRENTPAGE));
			 //4.取设置的默认数据
			 if(StringUtil.isNullorEmpty(orderFids)||StringUtil.isNullorEmpty(orderType)){
				 orderFids=WebConstants.DEFAULTORDERFIDS;
				 orderType=WebConstants.DEFAULTORDERTYPE;
				 currentPage="1";
			 }
		paramsContent.append(WebConstants.ORDERFIDS).append("=").append(orderFids);
		paramsContent.append(",").append(WebConstants.ORDERTYPE).append("=").append(orderType);
		paramsContent.append(",").append(WebConstants.CURRENTPAGE).append("=").append(currentPage);
		
		//遍历查询参数map
		HashMap<String,Object> hashmap=(HashMap<String, Object>) httpRequest.getAttribute("queryMap");
		if(hashmap!=null){
			Set<String> keySet = hashmap.keySet();
			for (String key : keySet) {
				Object valueObj=hashmap.get(key);
				if(valueObj!=null){
					if(valueObj.getClass().isArray()){
						String temp = ArrayUtil.getStringFormArr((String[])valueObj);
						if(isChinese(temp)){
							try {
								temp = java.net.URLEncoder.encode(temp,"utf-8");
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
							}
						}
						paramsContent.append(",").append(key).append("=").append(temp);
					}else{
						String temp = valueObj.toString();
						if(isChinese(temp)){
							try {
								temp = java.net.URLEncoder.encode(temp,"utf-8");
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
							}
						}
						paramsContent.append(",").append(key).append("=").append(temp);
					}
				}
			}
			
		}
		return paramsContent.toString();
	}
	
	public static final boolean isChinese(String strName) {  
		char[] ch = strName.toCharArray();  
		for (int i = 0; i < ch.length; i++) {  
		    char c = ch[i];  
		    if (isChinese(c)) {  
		       return true;  
		    }  
		}  
		return false;  
    }
	
	private static final boolean isChinese(char c) {  
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);  
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS  
		       || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS  
		       || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A  
		       || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION  
		       || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION  
		       || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {  
		   return true;  
		}  
	  return false;  
	}
}



